package ans.p219;

import test.Entrance;

import java.util.HashSet;
import java.util.Set;

/**
 * #      219
 * 名称   存在重复元素 II
 * 链接   https://leetcode-cn.com/problems/contains-duplicate-ii
 * 题解   [672](https://leetcode-cn.com/problems/contains-duplicate-ii/solution)
 * 通过率 41.4%
 * 难道   简单
 */

public class Solution {
    @Entrance
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        int slow = 0, fast = 0;
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (set.add(nums[fast])) {
                if (fast - slow >= k) {
                    set.remove(nums[slow]);
                    slow++;
                }
                fast++;
            } else {
                return true;
            }
        }
        return false;
    }
}